package com.xjcs.saplingplan.utils

import com.blankj.utilcode.util.TimeUtils
import com.nlf.calendar.Lunar
import com.nlf.calendar.LunarMonth
import com.nlf.calendar.Solar
import java.time.LocalDate
import java.util.Calendar
import java.util.Date

object FesLibUtils {

    fun getDateByCode(code: String, todaySolar: Solar): Pair<String, Int> {
        val dateStr: String
        val restDay = when(code){
            "chuXi" -> {
                val todayLunar = todaySolar.lunar
                val monthDay = LunarMonth.fromYm(todayLunar.year, 12).dayCount
                val targetLunar = Lunar(todayLunar.year, 12, monthDay)
                dateStr = "${targetLunar.yearInChinese}年${targetLunar.monthInChinese}月" + targetLunar.dayInChinese
                targetLunar.solar.subtract(todaySolar)
            }
            "qingMingJie" -> {
                val todayLunar = todaySolar.lunar
                var targetSolar = todayLunar.jieQiTable["清明"]!!
                if(targetSolar.isBefore(todaySolar)){
                    targetSolar = Lunar(
                        todayLunar.year + 1,
                        todayLunar.month,
                        todayLunar.day
                    ).jieQiTable["清明"]!!
                }
                dateStr = "${targetSolar.year}年${targetSolar.month}月${targetSolar.day}日"
                targetSolar.subtract(todaySolar)
            }
            "muQinJie" -> { //母亲节
                val todayLunar = todaySolar.lunar
                var targetSolar = getMotherDay(todayLunar.year)
                if(targetSolar.isBefore(todaySolar)){
                    targetSolar = getMotherDay(todayLunar.year + 1)
                }
                dateStr = "${targetSolar.year}年${targetSolar.month}月${targetSolar.day}日"
                targetSolar.subtract(todaySolar)
            }
            "fuQinJie" -> { //父亲节
                val todayLunar = todaySolar.lunar
                var targetSolar = getFatherDay(todayLunar.year)
                if(targetSolar.isBefore(todaySolar)){
                    targetSolar = getFatherDay(todayLunar.year + 1)
                }
                dateStr = "${targetSolar.year}年${targetSolar.month}月${targetSolar.day}日"
                targetSolar.subtract(todaySolar)
            }
            else -> {
                dateStr = ""
                0
            }
        }
        return Pair(dateStr, restDay)
    }

    // 获取母亲节（5月第二个星期日）
    private fun getMotherDay(year: Int): Solar {
        val daySolar = Solar(
            year, 5, 1
        )
        while (0 !== daySolar.week){
            daySolar.next(1)
        }
        daySolar.next(7)
        return daySolar
    }

    // 获取父亲节（6月第三个星期日）
    private fun getFatherDay(year: Int): Solar {
        val daySolar = Solar(
            year, 6, 1
        )
        while (0 !== daySolar.week){
            daySolar.next(1)
        }
        daySolar.next(14)
        return daySolar
    }
}